package com.ruix.app.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.SuperAdapter;
import com.ruix.app.adapter.superadapter.internal.SuperViewHolder;
import com.ruix.app.model.Lesson;
import com.ruix.app.model.LessonPostMember;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by hadexs on 2017/5/23.
 */

public class LessonSumupMembersAdapter extends BaseAdapter {


    private Context mContext;
    private ArrayList<LessonPostMember> members;
    private boolean contract = true;

    public LessonSumupMembersAdapter(Context context) {
        mContext = context;
    }

    public void setMembers(ArrayList<LessonPostMember> members) {
        this.members = members;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (members != null) {
            if (members.size() > 27) {
                if (contract) {
                    return 27;
                } else {
                    return members.size() + 1;
                }
            }
            return members.size();
        }
        return 0;
    }

    @Override
    public LessonPostMember getItem(int position) {
        if (members != null) {
            return members.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new LessonSumupMembersAdapter.ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_sumup_menber, null);
            viewHolder.userAvatar = (SimpleDraweeView) convertView.findViewById(R.id.sdv_user_avatar);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (position == getCount() - 1 && members.size() > 27 ) { //如果是最后一个
            viewHolder.userAvatar.setImageResource(R.drawable.post_member_more);
            viewHolder.userAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    contract = !contract;
                    notifyDataSetChanged();
                }
            });
        } else {
            LessonPostMember member = getItem(position);
            viewHolder.userAvatar.setImageURI(member.getAvatar());
            viewHolder.userAvatar.setOnClickListener(null);
        }
        return convertView;
    }


    static class ViewHolder {
        SimpleDraweeView userAvatar;

    }
}
